package com.project.gossip;

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.project.gossip.flyoutmenu.FlyOutContainer;
import com.project.gossip.recyclerview.GossipAdapter;
import com.project.gossip.recyclerview.GossipManager;

public class MainActivity extends ActionBarActivity {

	public static final String PREFS_NAME = "MyGossip";
	public static final String PREFS_NAME_lang = "language";

	public boolean TASK = false;
	public RecyclerView mRecyclerView;
	public GossipAdapter mAdapter;
	private static MainActivity mInstance;

	private FlyOutContainer root;

	public static MainActivity getInstance() {
		return mInstance;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SharedPreferences language = getSharedPreferences(PREFS_NAME_lang,
				Context.MODE_PRIVATE);
		Locale locale = new Locale(language.getString("lang", "de"));
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		this.getBaseContext().getResources().updateConfiguration(config, null);

		ActionBar actionBar = getSupportActionBar();

		// Creates an arrow that enables the user to quickly move to the parent
		// view
		// --> in this case should be used for all view's despite activity_main
		// to get back to activity_main
		// TODO: needs correct implementation
		// actionBar.setDisplayHomeAsUpEnabled(true);
		// actionBar.setDisplayUseLogoEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		// actionBar.setIcon(R.drawable.launcher_ic);

		this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
				R.layout.activity_main, null);
		this.setContentView(root);

		mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		if (GossipManager.getInstance().getRandomGossips(this) != null) {
			mAdapter = new GossipAdapter(GossipManager.getInstance()
					.getRandomGossips(this),
					R.layout.test_recyclerview_item_layout, this);
			mRecyclerView.setAdapter(mAdapter);
		}
		mInstance = this;

	}

	public void mainGossip(View v) {

		Toast t = Toast.makeText(this, "hallo", Toast.LENGTH_SHORT);
		t.show();
	}

	public void toggleMenu(View v) {
		this.root.toggleMenu();
	}

	public void startMyGossips(View v) {
		Intent i = new Intent(this, MyGossipsActivity.class);
		startActivity(i);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void refreshGossip() {
		GossipManager.getInstance().getNewRandomGossip(this);
		mAdapter = new GossipAdapter(GossipManager.getInstance()
				.getRandomGossips(this),
				R.layout.test_recyclerview_item_layout, this);
		mRecyclerView.setAdapter(mAdapter);

	}

	public void startNewGossip(View v) {
		Intent i = new Intent(this, NewGossipActivity.class);
		startActivity(i);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {

		case R.id.action_settings:
			openSettings();
			return true;
		case R.id.menu_button:
			toggleMenu(mRecyclerView);
			return true;
		case R.id.refresh_button:
			refreshGossip();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	private void openSearch(View v) {
		// TODO Auto-generated method stub

	}

	public void openSettings() {
		Intent intent = new Intent(this, Settings.class);
		startActivity(intent);
	}

	public void openSettings(View v) {

		openSettings();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.actionbar_main, menu);
		return true;
	}

}
